//
//  MBShowHeaderView.m
//  mibaotu
//
//  Created by 彭康 on 16/9/14.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBShowHeaderView.h"

@implementation MBShowHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self =[super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI {
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,ISIPAD?286*PERCEN:246*PERCEN)];
    topView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:topView];
    
    CGFloat marginE = 176*PERCEN;//每个图像之间的间距
    CGFloat imageViewI=160*PERCEN;//ipad图片大小
    
    CGFloat startL=(SCREEN_WIDTH-4*imageViewI-3*marginE)/2.0;//起始点
    CGFloat margin = 38*PERCEN;//间距
    CGFloat imageViewW = 140*PERCEN;
    CGFloat imageViewH = imageViewW;
  CGFloat  itemWidth = (SCREEN_WIDTH - 38*PERCEN)/4.;
    
    for (int i=0; i<4; i++) {
        
        UIImageView *headImgView = [[UIImageView alloc]init ];
    
 headImgView.frame= CGRectMake(
                                                                                margin+itemWidth*i, 30*PERCEN,  imageViewW, imageViewH);
        if (ISIPAD) {
            headImgView.frame= CGRectMake(
                                          startL+(imageViewI+marginE)*i, 40*PERCEN,  imageViewI, imageViewI);
        }
        headImgView.backgroundColor = [UIColor clearColor];
        [topView addSubview:headImgView];
        
        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth*i+margin, headImgView.bottom+20*PERCEN, imageViewW, 26*PERCEN)];
        if (ISIPAD) {
            titleLb.frame=CGRectMake( startL+(imageViewI+marginE)*i,  headImgView.bottom+20*PERCEN, imageViewI, 26*PERCEN);
        }
        titleLb.backgroundColor = [UIColor clearColor];
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.textColor = UIColorFromHex(0x333333);
        titleLb.font = [UIFont systemFontOfSize:13.f];
        if (ISIPAD) {
             titleLb.font = [UIFont systemFontOfSize:15.f];
        }
        [topView addSubview:titleLb];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemWidth*i+margin, 30*PERCEN, imageViewW, titleLb.bottom);
        if (ISIPAD) {
            bt.frame = CGRectMake(startL+(imageViewI+marginE)*i, 40*PERCEN, imageViewW, titleLb.bottom);

        }
     
        bt.backgroundColor = [UIColor clearColor];
        [topView addSubview:bt];

        if (i == 0) {
            UIImage *img = [UIImage imageNamed:@"zuixin_bg"];
            headImgView.image = img;
           
            titleLb.text = @"最新";

            [bt addTarget:self action:@selector(onSelectTheNewAction) forControlEvents:UIControlEventTouchUpInside];
        }
        else if (i == 1) {
            UIImage *img = [UIImage imageNamed:@"remen_bg"];
            headImgView.image = img;

            titleLb.text = @"热门";

            [bt addTarget:self action:@selector(onSelectTheHotAction) forControlEvents:UIControlEventTouchUpInside];
        }
        else if (i == 2) {
            UIImage *img = [UIImage imageNamed:@"dakaxiu_bg"];
            headImgView.image = img;

            titleLb.text = @"大咖秀";

            [bt addTarget:self action:@selector(onSelectTheShowAction) forControlEvents:UIControlEventTouchUpInside];
        }else  if(i == 3){
            UIImage *img = [UIImage imageNamed:@"bisaizhuanqu_bg"];
            headImgView.image = img;

            titleLb.text = @"比赛专区";

            [bt addTarget:self action:@selector(onSelectTheCompetationAction) forControlEvents:UIControlEventTouchUpInside];
       }
    }
}

#pragma mark -点击事件
- (void)onSelectTheHotAction {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForTheHotWithView:)]) {
        [_delegate onSelectItemForTheHotWithView:self];
    }
}

- (void)onSelectTheNewAction {
    
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForTheNewWithView:)]) {
        [_delegate onSelectItemForTheNewWithView:self];
    }
}

- (void)onSelectTheShowAction {
    
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectITemForTheShowWithView:)]) {
        [_delegate onSelectITemForTheShowWithView:self];
    }
}

- (void)onSelectTheCompetationAction {
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectITemForTheCompetationWithView:)]) {
        [_delegate onSelectITemForTheCompetationWithView:self];
    }
}


@end
